package com.wxp.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.modules.persistence.DynamicSpecifications;
import com.modules.utils.MenuMapping;
import com.modules.utils.ResourceMapping;
import com.modules.utils.ResourceMapping.Operation;
import com.modules.web.CRUDController;
import com.wxp.entity.User;
import com.wxp.service.UserService;

@Controller
@RequestMapping("/user/userInfo")
@MenuMapping(name = "用户信息", operator = "user10", parentOperator = "")
public class UserController extends CRUDController{
    private static Logger logger	= LoggerFactory.getLogger(UserController.class);

    /***
     * operator值一般设置
     * MenuMapping中     model01
     * 列表页面  model0101
     * 列表直接查询  model010101
     * 列表页面中相关button操作 model010101
     * 按钮相关辅助操作 model01010101
     * 
     * 同时注意  operator中的所有值都是作为主键进行存在的，不能够重复存在
     * 这样设置免去了  菜单的相关配置
     * */
    
    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    @RequiresPermissions ("user:list")//权限
    @ResourceMapping(name = "用户列表", operation = Operation.MENU, operator = "user1010", parentOperator = "user10")
    public String list(Model model){

        return "user/userList";
    }
    /*
     * 这个查询是跟页面按钮相关的操作是一个层级的
     * RequiresPermissions为了进行权限控制
     * **/
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ResourceMapping(name = "用户列表查询", operation = Operation.FUNCTION, operator = "user101010", parentOperator = "user1010")
    @RequiresPermissions ("user:list")//权限
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request) throws Exception {

        Page page = onPrepareQueryData(request);
        return  transferTo(page);
    }
    
    protected Page onPrepareQueryData(HttpServletRequest request) throws Exception {
    	Specification spec = DynamicSpecifications.bySearchFilter(request);//查询条件(基本查询条件)
    	//Specification spec = DynamicSpecifications.bySearchFilter(request,DynamicSpecifications.bulidSearchSpecFilter(new SearchFilter("EQ_name","王鹏"),new SearchFilter("or","EQ_name","撒地方")));//查询条件(基本查询条件)
        PageRequest pageRequest = DynamicSpecifications.buildPageRequest(request,null);//相关的分页
        Page<User> page = userService.findAll(spec,pageRequest);
        return page;//用来查询相关的数据
    }

    @RequiresPermissions ("user:add")//权限
    @ResourceMapping(name = "添加用户", operation = Operation.BUTTON, operator = "user101020", parentOperator = "user1010")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String showAddPage(Model model) {

        return "user/userEdit";
    }
    
    @RequiresPermissions ("user:view")//权限
    @ResourceMapping(name = "用户信息查看", operation = Operation.BUTTON, operator = "user101030", parentOperator = "user1010")
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String showViewPage(Model model) {
        model.addAttribute("states","view");
        return "user/userEdit";
    }

    /**
     * 返回编辑页面
     * @param id
     * @return 编辑页面
     */
    @RequiresPermissions ("user:edit")
    @ResourceMapping(name = "用户信息编辑页面", operation = Operation.BUTTON, operator = "user101040", parentOperator = "user1010")
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String showEditPage(String id,  Model model) {

        return "user/userEdit";
    }


    /**
     * 获取指定primary key的实体对象（数据库记录）
     */
    @RequiresPermissions ("user:edit")//权限
    @ResourceMapping(name = "用户信息编辑数据", operation = Operation.FUNCTION, operator = "user10104010", parentOperator = "user101040")//为了进行层级显示
    @RequestMapping(value = "/edit/{id}")
    @ResponseBody
    public User getEditData(@PathVariable String id) {
        User user = null;
        try {
            //user = userService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
           // logger.error(e.getMessage(), e);
        }
        return user;
    }
    /**
     * 保存是作为新建和编辑按钮的辅助性操作，可以认为保存是新建和保存下的子操作
     * 由于保存再新建和编辑都用到了，所以parentOperator 中可以用逗号隔开多个值
     * */
    @ResourceMapping(name = "用户信息保存", operation = Operation.FUNCTION, operator = "user10104020", parentOperator = "user101020,user101040")
    @RequiresPermissions (value={"user:edit", "user:add"},logical=Logical.OR)
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> save(User user) {
        Map<String, Object> tips = new HashMap<String, Object>();
        
        return tips;
    }
}
